<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class BlogController extends CI_Controller
{
    /*
     summary : 博客首页
     author : Steven Wang
     date : 29 Sep,2011

     params :
     <int>$pageid : 博客文章列表的页号，默认为第一页
     */
    public function index($pageid = 1)
    {
        // 获取指定页文章列表数据
        $this->load->model("blog/Articles","Articles",true);
        $articleIdList = $this->Articles->getArticleIdCollection($pageid);

        // 拼接文章列表数组部分的字符串
        $articleIdListStr = "";
        foreach($articleIdList as $articleId)
        {
            $articleIdListStr .= $articleId["Blog_Article_ID"] . ",";
        }
        $articleIdListStr = rtrim($articleIdListStr, ",");
                
        // 传入页头的数据
        // 页面标题,当前页号,文章ID列表数组,页面所需样式,页面所需脚本
        $pageInfo = array(
            "page_title" => "<CODE> IT - Coding My Life",
            "pageId" => $pageid,
            "styles" => array(
                "theme/blog/css/global.css",
                "theme/blog/css/content_articlelist.css"),
            "scripts" => array(
                "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js",
                "theme/blog/js/global.js",
                "theme/blog/js/lazyLoad.js")
        );
        // 传入文章列表的数据
        $articleIdList = array("articleIdList" => '[' . $articleIdListStr . ']');

        // 载入视图
        $this->load->view("blog/header",$pageInfo);
        $this->load->view("blog/articlelist",$articleIdList);
        $this->load->view("blog/sidebar");
        $this->load->view("blog/footer");
    }
    
    /*
     summary : 根据文章ID获取其相应的用于文章列表显示的内容
     author :　Steven Wang
     date : 03 Oct,2011

     params :
     <int>$id : 文章ID
    */
    public function getarticlelistitems($id = null)
    {
        if(!is_null($id))
        {
			$this->load->driver('cache', array('adapter' => 'file'));

			if ( !$articleJSON = $this->cache->get('article_item_'.$id) )
			{
				 // 载入文章表Model
				$this->load->model("blog/Articles","Articles",true);
				$article = $this->Articles->getArticleItem($id);
				
				if(!is_null($article))
				{
					$jsonArr = array(
						"articleID" => $article[0]['Blog_Article_ID'],
						"articleTitle" => $article[0]['Blog_Article_Title'],
						"articleAuthor" => $article[0]['Blog_Article_Author'],
						"publishDate" => $article[0]['Blog_Article_PublishDate'],
						"excerpt" => $article[0]['Blog_Article_Excerpt'],
						"visitNum" => $article[0]['Blog_Article_VisitNum'],
						"commentNum" => $article[0]['Blog_Article_CommentNum'],
						"isRecommend" => $article[0]['Blog_Article_IsRecommend']
					);
					$articleJSON = json_encode($jsonArr);
                    // 保存到Cache
                    $cache_timeout = $this->config->config['cache_timeout'];
					$this->cache->save('article_item_'.$id, $articleJSON, $cache_timeout);
				}
			}
			echo $articleJSON;
        }
    }

    /*
     summary : 根据文章ID获取其相应的分类名称
     author : Steven Wang
     date : 16 Oct,2011

     params :
     <int>$articleId : 文章ID
    */
    public function getarticlecategory($articleId)
    {
        if(!is_null($articleId))
        {
            $this->load->model("blog/V_Category","VCategory",true);
            $categoryName = $this->VCategory->getCategoryByArticleId($articleId);
            echo $categoryName["Blog_Category_Name"];
        }
    }

    /*
     summary : 根据文章ID获取其相应的标签
     author : Steven Wang
     date : 16 Oct,2011

     params :
     <int>$articleId : 文章ID
    */
    public function getarticletags($articleId)
    {
        if(!is_null($articleId))
        {
            // 读取数据库
            $this->load->model("blog/V_Tag","VTag",true);
            $tagArray = $this->VTag->getTagsByArticleId($articleId);
            // 根据结果数组组装javascript数组并返回
            $tags = '';
            $tags .= '[';
            foreach($tagArray as $tag)
            {
                $tags .= '"' . $tag['Blog_Tag_Name'] . '",';
            }
            $tags = rtrim($tags,',');
            $tags .= ']';
            echo $tags;
        }
    }

    /*
     summary : 根据文章ID获取详细文章内容
     author : Steven Wang
     date : 14 Oct,2011

     params : 文章ID
    */
    public function article($id)
    {
        // 检查Cache中是否存在当前文章的缓存
        $this->load->driver('cache', array('adapter' => 'file'));
        // 若不存在则读取数据库，并创建一份缓存
        if( !$article = $this->cache->get('article_'.$id) )
        {
            // 获取指定文章数据
            $this->load->model("blog/Articles","Articles",true);
            $article = $this->Articles->getArticle($id);
            // 获取文章的分类信息
            $this->load->model("blog/V_Category","VCategory",true);
            $categoryInfo = $this->VCategory->getCategoryNameByArticleId($id);
            // 获取文章的Tag信息
            $this->load->model("blog/V_Tag","VTag",true);
            $tagArray = $this->VTag->getTagNamesByArticleId($id);

            // 追加分类信息与Tag信息到article数组中
            $article['categoryInfo'] = $categoryInfo;
            $article['tagInfo'] = $tagArray;

            // 保存到Cache
            $cache_timeout = $this->config->config['cache_timeout'];
            $this->cache->save('article_'.$id, $article, $cache_timeout);
        }
        // 获取当前文章的上下篇文章索引信息
        $this->load->model("blog/V_ArticleIndex","ArticleIndex",true);
        $next = $this->ArticleIndex->getNext($id);
        $previous = $this->ArticleIndex->getPrevious($id);
        // 追加上下篇文章索引信息到article数组中
        $article['next'] = $next;
        $article['previous'] = $previous;

        // 传入页头的数据
        // 页面标题,页面所需样式,页面所需脚本
        $pageInfo = array(
            "page_title" => $article['Blog_Article_Title'] . " - <CODE> IT",
            "styles" => array(
                "theme/blog/css/global.css",
                "theme/blog/css/content_article.css"),
            "scripts" => array(
                "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js",
                "theme/blog/js/global.js",
                "theme/blog/js/article.js")
        );

        // 载入视图
        $this->load->view("blog/header",$pageInfo);
        $this->load->view("blog/article",$article);
        $this->load->view("blog/sidebar");
        $this->load->view("blog/footer");
    }

    public function getcomments($articleId)
    {
        if(!is_null($articleId))
        {
            // 获取评论数据
            $this->load->model("blog/Comments","Comments",true);
            $comments = $this->Comments->getCommentsByArticleID($articleId);

            if(isset($comments))
            {
                // 转化为JSON数组形式输出
                $commentJSON = json_encode($comments);
            }
            else
            {
                $commentJSON = null;
            }
            echo $commentJSON;
        }
    }

    public function category($categoryId,$pageid = 1)
    {
        if(!is_null($categoryId))
        {
            // 从文章-分类视图获取文章ID列表
            $this->load->model("blog/V_Category","VCategory",true);
            $articleIdList = $this->VCategory->getArticleIdCollectionByCategoryId($categoryId,$pageid);
            // 从分类表中获取分类名
            $this->load->model("blog/Category","Category",true);
            $categoryName = $this->Category->getCategoryNameByID($categoryId);

            // 拼接文章列表数组部分的字符串
            $articleIdListStr = "";
            foreach($articleIdList as $articleId)
            {
                $articleIdListStr .= $articleId["Blog_Article_ID"] . ",";
            }
            $articleIdListStr = rtrim($articleIdListStr, ",");
                    
            // 传入页头的数据
            // 页面标题,当前页号,文章ID列表数组,页面所需样式,页面所需脚本
            $pageInfo = array(
                "page_title" => "<CODE> IT - Coding My Life",
                "pageId" => $pageid,
                "styles" => array(
                    "theme/blog/css/global.css",
                    "theme/blog/css/content_articlelist.css"),
                "scripts" => array(
                    "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js",
                    "theme/blog/js/global.js",
                    "theme/blog/js/lazyLoad.js")
            );
            // 传入文章列表的数据
            $articleIdList = array(
                "articleIdList" => '[' . $articleIdListStr . ']',
                "category" => $categoryName);

            // 载入视图
            $this->load->view("blog/header",$pageInfo);
            $this->load->view("blog/articlelist",$articleIdList);
            $this->load->view("blog/sidebar");
            $this->load->view("blog/footer");
        }

        
    }
}

?>
